function Warn() {

}

Warn.prototype.run = function () {
    var self = this;
    self.listenWarnEvent();
};

Warn.prototype.listenWarnEvent = function () {
    //注意：导航 依赖 element 模块，否则无法进行功能性操作
    layui.use(['table', 'element', 'form', 'layer', 'laydate'], function () {
            var table = layui.table,
                form = layui.form,
                layer = layui.layer,
                laydate = layui.laydate,
                element = layui.element;

            var check_value = [];
            var check_dev = [];
            var check_manage = [];
            var check_day = [];
            var check_alarm = [];
            var times = [];
            var index = table.render({
                elem: '#warn-table',
                height: 'full', // 表格高度始终填满
                // even: true, //　列表隔行颜色
                size: 'lg',
                method: 'get', // 请求方法
                where: {
                    check_value: check_value,
                    check_dev: check_dev,
                    check_manage: check_manage,
                    check_day: check_day,
                    check_alarm: check_alarm,
                    times: times
                },
                url: '/video/warn_image/', // 数据接口
                page: true, // 开启分页　可传入laypage参数
                toolbar: '#warn-tool',
                limit: 6,
                limits: [6, 20, 50, 100, 200],
                text: {
                    none: '暂无数据...'
                },
                cols: [
                    [ // 表头　需要显示的数据列
                        {
                            type: 'checkbox',
                            fixed: 'left',
                            style: 'height: 111px',
                        },
                        {
                            field: 'image_id',
                            title: '预警编号',
                        },
                        {
                            field: 'ip',
                            title: '监控ip',
                            sort: true,
                        },
                        {
                            field: 'type',
                            title: '预警类型',
                            sort: true,
                        },
                        {
                            field: 'add_time',
                            title: '发生时间',
                            sort: true,
                        },
                        {
                            field: 'image',
                            title: '事件图片',
                            templet: '#warns-image',
                            align: 'center',
                        },
                        {
                            field: 'id',
                            title: '事件处理',
                            fixed: 'right',
                            align: 'center',
                            toolbar: '#warn-make',
                        },
                    ]
                ],
            });

            // 多选框处理
            table.on('toolbar(warnlist)', function (obj) {
                switch (obj.event) {
                    // 一键跳过
                    case 'make-pass':
                        var check_data = table.checkStatus(obj.config.id);
                        if (check_data.data.length == 0) {
                            layer.alert('请先勾选需要跳过的行...');
                        } else {
                            var data_list = new Array();
                            for (var i = 0; i < check_data.data.length; i++) {
                                data_list.push(check_data.data[i]['id'])
                            }
                            var data_str = data_list.join(',');
                            layer.open({
                                resize: false,
                                content: '确认跳过吗？',
                                btn: ['确认', '取消'],
                                yes: function (index, layero) {
                                    $.ajax({
                                        type: 'post',
                                        url: '/warn_list/rec_data/',
                                        data: {
                                            'data_list': data_str
                                        },
                                        success: function (result) {
                                            if (result['code'] === 200) {
                                                layer.alert('丢弃成功，您可在统计备案页面找到他们哦~');
                                                setTimeout('parent.location.reload()', 1000);
                                            } else {
                                                var messageObject = result['message'];
                                                if (typeof messageObject == 'string') {
                                                    layer.alert(messageObject);
                                                } else {
                                                    for (var key in messageObject) {
                                                        var messages = messageObject[key];
                                                        var message = messages[0];
                                                        layer.alert(message);
                                                    }
                                                }
                                            }
                                        },
                                        error: function () {
                                            layer.alert('丢弃失败，请重试...');
                                        }
                                    });
                                },
                                btn2: function (index) {
                                    layer.close(index);
                                    parent.location.reload();
                                }
                            });
                        }
                        break;

                    // 任务类型筛选
                    case 'manage-select':
                        layer.open({
                            resize: false,
                            type: 1,
                            title: '请勾选任务类型',
                            content: $('#layer-manage-select'),
                            offset: ['150px', '300px'],
                            area: ['300px', '200px'],
                            btn: ['确认', '取消'],
                            yes: function (index) {
                                obj = document.getElementsByName('manage');
                                check_val = [];
                                for (k in obj) {
                                    if (obj[k].checked) {
                                        check_val.push(obj[k].value);
                                    }
                                }
                                var new_val = check_val.join(',');
                                check_manage = new_val;

                                if (check_manage.length != 0) {
                                    table.reload('warn-table', {
                                        where: {
                                            check_manage: check_manage
                                        },
                                        url: '/warn_list/rec_data/', // 数据接口
                                    });
                                } else if (check_manage.length == 0) {
                                    parent.location.reload();
                                }
                                layer.close(index);
                            }
                        });
                        break;

                    // 预警类型筛选
                    case 'warn-select':
                        layer.open({
                            resize: false,
                            type: 1,
                            title: '请勾选预警类型',
                            content: $('#layer-warn-select'),
                            offset: ['150px', '400px'],
                            area: ['300px', '200px'],
                            btn: ['确认', '取消'],
                            yes: function (index) {
                                obj = document.getElementsByName('warn');
                                check_val = [];
                                for (k in obj) {
                                    if (obj[k].checked) {
                                        check_val.push(obj[k].value);
                                    }
                                }
                                var new_val = check_val.join(',');
                                check_value = new_val;

                                if (check_value.length != 0) {
                                    table.reload('warn-table', {
                                        where: {
                                            check_value: check_value
                                        },
                                        url: '/warn_list/rec_data/', // 数据接口
                                    });
                                } else if (check_value.length == 0) {
                                    parent.location.reload();
                                }
                                layer.close(index);
                            }
                        });
                        break;

                    // 事件类型
                    case 'alarm-select':
                        layer.open({
                            resize: false,
                            type: 1,
                            title: '请勾选条件',
                            content: $('#layer-alarm-select'),
                            offset: ['150px', '500px'],
                            area: ['300px', '200px'],
                            btn: ['确认', '取消'],
                            yes: function (index) {
                                obj = document.getElementsByName('alarm');
                                check_alarm = [];
                                for (k in obj) {
                                    if (obj[k].checked) {
                                        check_alarm.push(obj[k].value);
                                    }
                                }
                                var new_alarm = check_alarm.join(',');
                                check_alarm = new_alarm;

                                if (check_alarm.length != 0) {
                                    table.reload('warn-table', {
                                        where: {
                                            check_alarm: check_alarm
                                        },
                                        url: '/warn_list/rec_data/', // 数据接口
                                    });
                                } else if (check_day.length == 0) {
                                    parent.location.reload();
                                }
                                layer.close(index);
                            }
                        });
                        break;

                    // 昼夜筛选
                    case 'day-select':
                        layer.open({
                            resize: false,
                            type: 1,
                            title: '请勾选条件',
                            content: $('#layer-day-select'),
                            offset: ['150px', '600px'],
                            area: ['300px', '200px'],
                            btn: ['确认', '取消'],
                            yes: function (index) {
                                obj = document.getElementsByName('day');
                                check_day = [];
                                for (k in obj) {
                                    if (obj[k].checked) {
                                        check_day.push(obj[k].value);
                                    }
                                }
                                var new_val = check_day.join(',');
                                check_day = new_val;

                                if (check_day.length != 0) {
                                    table.reload('warn-table', {
                                        where: {
                                            check_day: check_day
                                        },
                                        url: '/warn_list/rec_data/', // 数据接口
                                    });
                                } else if (check_day.length == 0) {
                                    parent.location.reload();
                                }
                                layer.close(index);
                            }
                        });
                        break;

                    // 时间筛选
                    case 'time-select':
                        layer.open({
                            resize: false,
                            type: 1,
                            title: '筛选时间',
                            content: $('#layer-time-select1'),
                            offset: ['150px', '600px'],
                            area: ['600px', '480px'],
                            // btn: ['确认', '取消'],
                            success: function (layero, index) {
                                laydate.render({
                                    elem: '#time1',
                                    range: true,
                                    done: function (value, date, endDate) {
                                        times = value;
                                        if (times.length != 0) {
                                            table.reload('warn-table', {
                                                where: {
                                                    times: times
                                                },
                                                url: '/warn_list/rec_data/', // 数据接口
                                            });
                                        } else if (times.length == 0) {
                                            parent.location.reload();
                                        }
                                        layer.close(index);
                                    }
                                });
                            },
                        });
                        break;

                    // 监控点筛选
                    case 'dev-select':
                        layer.open({
                            resize: false,
                            type: 1,
                            title: '请勾选监控点',
                            content: $('#layer-dev-select'),
                            offset: ['150px', '690px'],
                            area: ['250px', '400px'],
                            btn: ['确认', '取消'],
                            success: function () {
                                $.ajax({
                                    type: 'get',
                                    url: '/public/select_dev/',
                                    data: {},

                                    success: function (result) {
                                        if (result['code'] === 200) {
                                            var html = template('select-dev', {
                                                'data': result['data']
                                            });
                                            document.getElementById('add-select-dev').innerHTML = html;
                                            form.render();

                                        } else {
                                            var messageObject = result['message'];
                                            if (typeof messageObject == 'string') {
                                                layer.alert(messageObject);
                                            } else {
                                                for (var key in messageObject) {
                                                    var messages = messageObject[key];
                                                    var message = messages[0];
                                                    layer.alert(message);
                                                }
                                            }
                                        }
                                    },
                                    error: function () {
                                        layer.alert('获取失败，请重试...');
                                    }
                                })
                            },
                            yes: function (index) {
                                obj = document.getElementsByName('dev');
                                check_val = [];
                                for (k in obj) {
                                    if (obj[k].checked) {
                                        check_val.push(obj[k].value);
                                    }
                                }
                                var new_dev = check_val.join(',');
                                check_dev = new_dev;
                                if (check_dev.length != 0) {
                                    table.reload('warn-table', {
                                        where: {
                                            check_dev: check_dev
                                        },
                                        url: '/warn_list/rec_data/', // 数据接口
                                    });
                                }
                                layer.close(index);
                            }
                        });
                        break;

                    // 导出数据
                    case 'make-down':
                        layer.confirm('确认导出吗?', function (index) {
                            var url = '/warn_list/return_excel/?type_id=4' + '&check_value=' + check_value + '&check_dev=' + check_dev +
                                check_dev + '&check_manage=' + check_manage + '&check_alarm=' + check_alarm + '&check_day=' + check_day + '&times=' + times;
                            window.location.href = url;
                            layer.close(index);
                        });
                        break;
                }
            });

            //　监听工具条
            table.on('tool(warnlist)', function (obj) {
                var data = obj.data; // 获取当前行数据
                var layEvent = obj.event; // lay-event的值

                if (layEvent === 'warn-del') {
                    layer.confirm('确认丢弃吗?', function (index) {
                        $.ajax({
                            type: 'post',
                            url: '/warn_list/rec_data/',
                            data: {
                                'id': data['id']
                            },
                            success: function (result) {
                                if (result['code'] === 200) {
                                    layer.alert('丢弃成功，您可在统计备案页面找到他们哦~');
                                    setTimeout('parent.location.reload()', 1000);
                                } else {
                                    var messageObject = result['message'];
                                    if (typeof messageObject == 'string') {
                                        layer.alert(messageObject);
                                    } else {
                                        for (var key in messageObject) {
                                            var messages = messageObject[key];
                                            var message = messages[0];
                                            layer.alert(message);
                                        }
                                    }
                                }
                            },
                            error: function () {
                                layer.alert('删除失败，请重试...');
                            }
                        });
                        layer.close(index);
                    });
                }

                else if (layEvent === 'view-warns') {
                    let warn_url = data['image'];
                    if (warn_url.length <= 4) {
                        layer.alert('手动预警无标注图片,请选择智能预警!');
                    } else {
                        // let image_sign = eval(data['image_sign']);
                        let warn_url = data['image'];
                        json_data = {
                            'data': [
                                {'src': warn_url}
                            ]
                        };
                        layer.photos({
                            photos: json_data,
                            anim: 5
                        });
                    }
                }

                else if (layEvent === 'warn-save') {
                    layer.confirm('确认上报至城管系统吗?', function (index) {
                        $.ajax({
                            type: 'post',
                            url: '/public/send_data/',
                            data: {
                                'id': data['id'],
                                'type_name': data['type_name'],
                                'video_url': data['video_url'],
                                'images': JSON.stringify(data['images']),
                                'event_id': data['event_id'],
                                'map_name': data['map_name'],
                                'warn_url': data['warn_url'],
                                'dev_id': data['dev_id']
                            },
                            success: function (result) {
                                if (result['code'] === 200) {
                                    layer.alert('上报成功，您可在统计备案页面找到他们哦~');
                                    setTimeout('parent.location.reload()', 1000);
                                } else {
                                    var messageObject = result['message'];
                                    if (typeof messageObject == 'string') {
                                        layer.alert(messageObject);
                                    } else {
                                        for (var key in messageObject) {
                                            var messages = messageObject[key];
                                            var message = messages[0];
                                            layer.alert(message);
                                        }
                                    }
                                }
                            }
                        })
                    });
                } else if (layEvent === 'view-image') {
                    // 弹窗查看图片
                    $.ajax({
                        type: 'get',
                        url: '/warn_list/warn_image/',
                        data: {
                            'id': data['id']
                        },
                        success: function (result) {
                            if (result['code'] === 200) {
                                var html = template('add-image', {
                                    'data': result['data']
                                });
                                document.getElementById('image-views').innerHTML = html;
                                var image_obj = $('#image0');
                                var image_views = $('#image-views');
                                var video_views = $('#video-views');
                                image_obj.click();
                                layer.photos({
                                    photos: '#image-views',
                                    anim: 5,
                                    end: function (index) {
                                        image_views.remove();
                                        video_views.append('<div id="image-views" style="display: none"></div>');
                                    }
                                });
                            } else {
                                var messageObject = result['message'];
                                if (typeof messageObject == 'string') {
                                    layer.alert(messageObject);
                                } else {
                                    for (var key in messageObject) {
                                        var messages = messageObject[key];
                                        var message = messages[0];
                                        layer.alert(message);
                                    }
                                }
                            }
                        },
                        error: function () {
                            layer.alert('本预警暂无图片，请重试...');
                        }
                    });
                } else if (layEvent === 'view-video') {
                    // 弹窗查看视频
                    layer.open({
                        type: 1,
                        title: '播放预警视频',
                        resize: false,
                        area: ['700px', '450px'],
                        content: $('#video-views'),
                        btn: '',
                        success: function () {
                            var video_url = data['video_url']; // 获取播放地址
                            var player = videojs('play-video');
                            player.ready(function () {
                                var myPlayer = this;
                                myPlayer.reset();
                                myPlayer.src({
                                    type: "video/mp4",
                                    src: video_url
                                });
                                myPlayer.load(video_url); // 这个需要从新加载地址...
                                myPlayer.play(); // 点击播放
                            });
                        },
                        yes: function (index, layero) {
                            layer.close(index);
                        }
                    })
                }
            });
        }
    );
};


$(function () {
    var warn = new Warn();
    warn.run();
});